﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreData;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using SchoolManagementSystem.Controls;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmSelectProduct : DevExpress.XtraEditors.XtraForm
    {
        public List<long> ProductSeletedIDs { get; set; }
        public List<long> TempSelect { get; set; }
        public int TypeOfLoad { get; set; }
        public long ProviderIDs { get; set; }
        string SearchNullTI = "OR (TI.Code IS NULL OR TI.Name IS NULL)";
        string SearchNullP = "OR (P.Code IS NULL OR P.Name IS NULL)";
        int FocusedRow
        {
            get
            {
                return gridView1.FocusedRowHandle;
            }
        }

        public frmSelectProduct()
        {
            InitializeComponent();
            gridView1.SetFocusedValue(0);
        }
        

        #region Method

        /// <summary>
        /// Xử lý cho phiếu nhập
        /// </summary>
        /// <param name="ListProductID"></param>
        /// <param name="ProviderID"></param>
        /// <param name="search"></param>
        private void FuncIn(List<long> ListProductID, long ProviderID, string search)
        {
            TypeOfLoad = 1;
            var model = RegisterService.RegisterServices.ProductService;
            gridControl1.DataSource = model.GetProductList_frmSelectedProduct(ListProductID, ProviderID, search);
            gridControl1.RefreshDataSource();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";
            gridView1.ExpandAllGroups();
            ColInStockQuantity.Visible = false;

        }

        /// <summary>
        /// Xử lý cho phiếu xuất
        /// </summary>
        /// <param name="ListProductID"></param>
        /// <param name="search"></param>
        private void FuncOut(List<long> ListProductID, string search)
        {
            TypeOfLoad = 2;
            var model = RegisterService.RegisterServices.ProductService;
            gridControl1.DataSource = model.GetProductList_frmSelectedProduct(ListProductID, search);
            gridControl1.RefreshDataSource();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";
            gridView1.ExpandAllGroups();
        }

        public DialogResult ShowDialog(List<long> ListProductID, long ProviderID)
        {
            FuncIn(ListProductID, ProviderID, SearchNullP);
            TempSelect = ListProductID;
            ProviderIDs = ProviderID;

            this.ShowDialog();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            return this.DialogResult;
        }

        public DialogResult ShowDialog(List<long> ListProductID)
        {

            FuncOut(ListProductID, SearchNullTI);
            TempSelect = ListProductID;
            this.ShowDialog();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            return this.DialogResult;
        }
        
        //Khoi tao ton dau
        public void FuncBeginStock(List<long> ListProductID, int TypeLoad, string search)
        {
            TypeOfLoad = TypeLoad;
            TempSelect = ListProductID;
            var model = RegisterService.RegisterServices.ProductService;
            if (TypeOfLoad == 1)
            {
                gridControl1.DataSource = model.GetProductBeginStock_frmSelectedProduct(ListProductID, search);
            }
            gridControl1.RefreshDataSource();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";

        }

        public DialogResult ShowDialog(List<long> ListProductID, int TypeLoad)
        {
            FuncBeginStock(ListProductID, TypeLoad, SearchNullTI);

            this.ShowDialog();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            return this.DialogResult;
        }

        private void IsSelected(int row)
        {
            var check = (bool)gridView1.GetRowCellValue(row, "SelectRowExt");
            if (Convert.ToDecimal(gridView1.GetRowCellValue(row, "InStockQuantityExt")) <= 0 && check)
            {
                gridView1.SetRowCellValue(row, "SelectRowExt", false);
                MessageControl.ShowMessage(gridView1.GetRowCellValue(row, "Name") + " đã hết hàng", "Thông báo", MessageControl.Icon.Warning);
                return;
            }
        }

        private void SetColorForColumn(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (gridView1.RowCount > 0)
            {
                switch (e.Column.FieldName)
                {
                    case "InStockQuantityExt":
                        var inStock = Convert.ToDecimal(gridView1.GetRowCellValue(e.RowHandle, "InStockQuantityExt"));
                        if (inStock <= 0)
                        {
                            e.Appearance.BackColor = Color.Red;
                            e.Appearance.ForeColor = Color.White;
                        }
                        break;
                }
            }
        }

        #endregion

        #region Event
        private void cmdselected_Click(object sender, EventArgs e)
        {
          
            ProductSeletedIDs = new List<long>();
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                var check = (bool)gridView1.GetRowCellValue(i, "SelectRowExt");
                if (check == true)
                {
                    ProductSeletedIDs.Add((long)gridView1.GetRowCellValue(i, "ID"));
                }
            }
            if (ProductSeletedIDs.Count <= 0)
            {
                MessageControl.ShowMessage("Bạn chưa chọn sản phẩm nào, vui lòng chọn sản phẩm", "Cảnh báo", MessageControl.Icon.Warning);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                var _row = gridView1.GetSelectedRows();
                for (int i = 0; i < _row.Length; i++)
                {
                    if (Convert.ToDecimal(gridView1.GetRowCellValue(_row[i], "InStockQuantityExt")) > 0)
                    {
                        if (((bool)gridView1.GetRowCellValue(_row[i], "SelectRowExt")) == true)
                        {
                            gridView1.SetRowCellValue(_row[i], "SelectRowExt", false);
                        }
                        else
                        {
                            gridView1.SetRowCellValue(_row[i], "SelectRowExt", true);
                        }
                    }
                }


            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
           
        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            SetColorForColumn(e);
        }
        
        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (TypeOfLoad == 2 && e.RowHandle>=0)//Xuat kho
            {
                var _erow = e.RowHandle;
               
                if (((bool)gridView1.GetRowCellValue(_erow, "SelectRowExt")) == true)
                {
                    gridView1.SetRowCellValue(_erow, "SelectRowExt", false);

                }
                else
                {
                    IsSelected(e.RowHandle);
                    gridView1.SetRowCellValue(_erow, "SelectRowExt", true);
                }
                gridView1.UpdateCurrentRow();

            }
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (TypeOfLoad == 2 && e.RowHandle >= 0)//Xuat kho
               IsSelected(e.RowHandle);
        
        }
   
        private void btnFilter_Click(object sender, EventArgs e)
        { 
            string SearchNullTI="";
            string SearchNullP = "";   
            if(!string.IsNullOrEmpty(txtcode.Text) && string.IsNullOrEmpty(txtName.Text))
            {
                    SearchNullTI = string.Format(" AND TI.Code like \'%{0}%\'", txtcode.Text);
                    SearchNullP = string.Format("AND P.Code like \'%{0}%\'", txtcode.Text); 
            }
            else if (string.IsNullOrEmpty(txtcode.Text) && !string.IsNullOrEmpty(txtName.Text))
            {
                SearchNullTI = string.Format("AND TI.Name like \'%{0}%\'", txtName.Text);
                SearchNullP = string.Format("AND P.Name like \'%{0}%\'", txtName.Text);
            }
            else
            {
                SearchNullTI = string.Format("AND (TI.Code like \'%{0}%\' OR TI.Name like \'%{1}%\')", txtcode.Text, txtName.Text);
                SearchNullP = string.Format("AND (P.Code like \'%{0}%\' OR P.Name like \'%{1}%\')", txtcode.Text, txtName.Text); 
            }
            if (!string.IsNullOrEmpty(SearchNullTI))
            {
                if (TypeOfLoad == 1)
                {
                    FuncIn(TempSelect, ProviderIDs, SearchNullP);
                }
                else if (TypeOfLoad == 2)
                {
                    FuncOut(TempSelect, SearchNullTI);
                }
                else
                {
                    FuncBeginStock(TempSelect, TypeOfLoad, SearchNullTI);
                }
            }
            
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtName.Text = string.Empty;
            txtcode.Text = string.Empty;
        }

        #endregion
    }
}
